﻿using Common.DataModels;
using Microsoft.AspNetCore.Mvc;
using PLC.Domain.Dto;
using PLC.Domain.Models;
using PLC.Domain.Service.Service;

namespace PLC.API.Controllers
{
    [Route("api/plc-operation")]
    [ApiController]
    public class PLCOperationController : BaseApiController
    {
        private readonly PLCOperationService _plcOperationService;

        public PLCOperationController(PLCOperationService plcOperationService)
        {
            _plcOperationService = plcOperationService?? throw new ArgumentNullException(nameof(plcOperationService));
        }
        /// <summary>
        /// 写入plc
        /// </summary>
        /// <param name="plcOperationModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel> WritePLCData(PLCOperationModel plcOperationModel)
        {
            await _plcOperationService.WritePLCData(plcOperationModel);
            return Success("写入成功");
        }
        /// <summary>
        /// 读取plc数据
        /// </summary>
        /// <param name="plcOperationModel"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PLCOperationModel>> ReadPLCData(PLCOperationModel plcOperationModel)
        {
            var data =  await _plcOperationService.ReadPLCData(plcOperationModel);
            return Success(data, "读取成功");
        }
       /// <summary>
       /// 切换配方
       /// </summary>
       /// <param name="formula"></param>
       /// <returns></returns>
        [HttpPut("formula")]
        public async Task<MessageModel> SwitchRecipe([FromQuery]int formula)
        {
            await _plcOperationService.SwitchRecipe(formula);
            return Success("切换成功");
        }
        /// <summary>
        /// 获取公式
        /// </summary>
        /// <returns></returns>
       [HttpGet("formula")]
       public async Task<MessageModel<IList<DtoFormula>>> FindFormula()
       {
           var data = await _plcOperationService.FindFormulas();
           return Success(data);
       }
        /// <summary>
        /// 写入任务号
        /// </summary>
        /// <param name="taskCode"></param>
        /// <returns></returns>
        [HttpPut("task-code")]
        public async Task<MessageModel> ChangeTaskCode([FromQuery]string taskCode)
        {
            await _plcOperationService.ChangeTaskCode(taskCode);
            return Success("写入成功");
        }
    }
}
